import type { ITjFormItemProps } from '@/tjComponents/TjForm/TjFormItem';
import type { ArtColumn } from 'ali-react-table';
import { Button, DatePicker } from 'antd';
import _ from 'lodash';

const monthData = [
  { value: 1, label: '1月' },
  { value: 2, label: '2月' },
  { value: 3, label: '3月' },
  { value: 4, label: '4月' },
  { value: 5, label: '5月' },
  { value: 6, label: '6月' },
  { value: 7, label: '7月' },
  { value: 8, label: '8月' },
  { value: 9, label: '9月' },
  { value: 10, label: '10月' },
  { value: 11, label: '11月' },
  { value: 12, label: '12月' },
];

const timeOrderTypeArr = [
  { value: 1, label: '尖峰' },
  { value: 2, label: '高峰' },
  { value: 3, label: '平段' },
  { value: 4, label: '低谷' },
  { value: 5, label: '深谷' },
];

const filterItem: ITjFormItemProps[] = [{ name: 'year', render: <DatePicker picker="year" /> }];

const columns = ({
  del,
  edit,
}: {
  del: (index: number) => void;
  edit: (row: any, index: number) => void;
}): ArtColumn[] => [
  {
    name: '月份',
    code: 'month',
    align: 'center',
    render: (value) => {
      let str = '';
      if (_.isArray(value) && value.length > 0) {
        const arr = [];
        for (let i = 0; i < value.length; i++) {
          const item = _.find(monthData, (n: any) => n.value === value[i]);
          arr.push(item.label);
        }
        str = arr.join(',');
      }
      return str;
    },
  },
  {
    name: '时段',
    code: 'timeOrderType',
    align: 'center',
    width: 120,
    render: (value) => {
      return value ? timeOrderTypeArr[value - 1].label : '-';
    },
  },
  {
    name: '时点',
    code: 'programStandard',
    align: 'center',
    render: (value) => {
      return _.isArray(value) && value.length > 0 ? value.join(',') : '-';
    },
  },
  {
    name: '平台参数设置',
    code: 'timeOrder',
    align: 'center',
    render: (value) => {
      return value ? value : '-';
    },
  },
  {
    name: '小时数',
    code: 'hour',
    align: 'center',
    width: 100,
    render: (value) => {
      return value ? value : '-';
    },
  },
  {
    name: '操作',
    code: 'opts',
    align: 'center',
    width: 140,
    render: (value, row, index) => {
      return (
        <>
          <Button type="link" onClick={() => edit(row, index)}>
            编辑
          </Button>
          <Button type="link" onClick={() => del(index)}>
            删除
          </Button>
        </>
      );
    },
  },
];

function checkMonth(arr: number[]) {
  const expected = _.range(1, 13);
  let missing = [];
  let extra = [];
  missing = _.difference(expected, arr);
  extra = _.difference(arr, expected);
  return {
    missing,
    extra,
  };
}

export { columns, monthData, timeOrderTypeArr, checkMonth, filterItem };
